package ltd.jdsoft.cute.channel.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ltd.jdsoft.cute.cache.CacheClient;
import ltd.jdsoft.cute.cache.CacheKeyUtil;
import ltd.jdsoft.cute.cache.callback.GetCallback;
import ltd.jdsoft.cute.channel.dao.OrgnizationDAO;
import ltd.jdsoft.cute.channel.model.Orgnization;

/**
 * 机构service层
 * 
 * @author zhengzhq
 *
 */
@Service
public class OrgnizationService {
  @Autowired
  private OrgnizationDAO orgnizationDAO;
  public static final CacheKeyUtil keyUtil = new CacheKeyUtil(Orgnization.class);

  public void add(Orgnization orgnization) {
    orgnizationDAO.add(orgnization);
  }

  public void update(Orgnization orgnization) {
    orgnizationDAO.update(orgnization);
    CacheClient.remove(keyUtil.getRecKey("." + orgnization.getId()));
    CacheClient.remove(keyUtil.getRecKey(".cooperator." + orgnization.getCooperator()));
    // 清空缓存的public key
    CacheClient.remove(keyUtil.getRecKey(".pk." + orgnization.getId()));
  }

  public Orgnization get(long id) {
    return CacheClient.get(keyUtil.getRecKey("." + id), new GetCallback<Orgnization>() {

      @Override
      public String getCacheGroup() {
        return null;
      }

      @Override
      public Orgnization getObject() {
        return orgnizationDAO.get(id);
      }
    });
  }

  public Orgnization getByCooperator(String cooperator) {
    return CacheClient.get(keyUtil.getRecKey(".cooperator." + cooperator),
        new GetCallback<Orgnization>() {

          @Override
          public String getCacheGroup() {
            return null;
          }

          @Override
          public Orgnization getObject() {
            return orgnizationDAO.getByCooperator(cooperator);
          }
        });
  }

  public List<Orgnization> getOrgnizationList() {
    return orgnizationDAO.getOrgnizationList();
  }
}
